package com.chenglu.android.restaurant.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chenglu.android.R;
import com.chenglu.android.common.utils.GlideUtils;
import com.chenglu.android.index.bean.IndexRecomBean;
import com.chenglu.android.index.bean.PageListBean;

import java.util.List;

public class TopicCateAdapter extends RecyclerView.Adapter<TopicCateAdapter.TopicViewHolder> {

    private List<PageListBean.Data.Records> mData;
    private OnItemClickListener onItemClickListener;
    private Context mContext;
    private int columnCount = 4;

    public TopicCateAdapter(Context context, List<PageListBean.Data.Records> data) {
        this.mData = data;
        this.mContext = context;
    }

    @Override
    public TopicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TopicViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_index_top_recom, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull TopicViewHolder holder, final int position) {
        final PageListBean.Data.Records item = mData.get(position);
        if (item == null) {
            return;
        }
        holder.title.setText(item.getCategoryName());
        GlideUtils.setImageView(mContext,item.getIcon(),holder.ivIconCenter,8);

//        Drawable top = mContext.getResources().getDrawable(item.getIcon() == 0 ? R.mipmap.icon_home_99 : item.getIcon());
//        holder.title.setCompoundDrawablesWithIntrinsicBounds(null, top, null, null);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                if (onItemClickListener != null) {
                    onItemClickListener.onTopicItemClick(item,item.getCategoryName(),item.getId());
                }
            }
        });

        GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
        int screenWidth = mContext.getResources().getDisplayMetrics().widthPixels; //屏幕宽度
        params.width = screenWidth / columnCount;
        holder.itemView.setLayoutParams(params);

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }

    public class TopicViewHolder extends RecyclerView.ViewHolder {

        public TextView title;
        public ImageView ivIconCenter;

        public TopicViewHolder(View view) {
            super(view);
            title = view.findViewById(R.id.title);
            ivIconCenter = view.findViewById(R.id.iv_icon_center);
        }
    }

    public interface OnItemClickListener {

        public void onTopicItemClick(PageListBean.Data.Records position, String categoryName, String id);

    }

}